<template>
    <div id='finance-reject'>
        <h1 class="card-title">
            <!-- <p><span>拒绝放款记录</span></p> -->
            <div class="card-title__button-box">
                <div class="form-item__value">
                    <input type="text" placeholder="请输入手机号码" v-model='phone'>
                </div>
                <div class="card-title__button" @click='getList(1)'>搜索</div>
            </div>
        </h1>
        
        <loading v-if='loading'></loading>
        <p class='no-data' v-if='!loading && !records.length'>暂无数据</p>
        <div class="table-list" v-if='!loading && records.length'>
            <table>
                <thead class="table-list__head">
                    <tr>
                        <th>序号</th>
                        <th>借款人</th>
                        <th>手机号码</th>
                        <th>借款金额</th>
                        <th>借款时长</th>
                        <th>到账金额</th>
                        <th>服务费</th>
                        <th>应还款日</th>
                        <th>实际还款日</th>
                        <th>已逾期天数</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody class='table-list__body have-border'>
                    <tr v-for='item in records'>
                        <td>{{ item.number || placeholder }}</td>
                        <td>{{ item.name || placeholder }}</td>
                        <td>{{ item.phone || placeholder }}</td>
                        <td>{{ item.lendAmount | toYuan }}</td>
                        <td>{{ item.lendCycle || placeholder }}天</td>
                        <td>{{ item.lendRealAmount | toYuan }}</td>
                        <td>{{ item.serviceAmount | toYuan }}</td>
                        <td>{{ item.needRepayTime || placeholder }}</td>
                        <td>{{ item.repayTime || placeholder }}</td>
                        <td>{{ item.overDueDays }}</td>
                        <td>{{ item.status || placeholder }}</td>
                        <td>
                            <div class='option-box'>
                                <span class='option-button' @click='goDetail(item.id)'>查看</span>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <pagination
                :current-page='listInfo.pageNum'
                :total-page='listInfo.totalPage'
                @pageChange='getList'></pagination>
        </div>
    </div>
</template>

<script>
    import { Promise } from 'es6-promise'
    import loading from '../components/Loading.vue'
    import buttonLoading from '../components/ButtonLoading.vue'
    import pagination from '../components/Pagination.vue'
    import dateFormat from '../filters/filter-dateFormat'
    import toYuan from '../filters/filter-toYuan'
    import { rules } from '../utils/utils'
    import  { mapGetters, mapActions }  from 'vuex'
    import { financeApi } from '../api/api'
    import moment from 'moment'


    export default {
        components:{
            loading,
            buttonLoading,
            pagination,
        },
        filters:{
            dateFormat,
            toYuan,
            booleanToStr(boolean){
                if(boolean === true){
                    return '是'
                }
                else{
                    return '否'
                }
            }
        },
        data() {
            return {
                placeholder:'--',
                phone:'',
                listInfo:{},
                records:[],
                loading:false,
                type:''
            }
        },
        computed:{
            ...mapGetters([
                'msgTip',
                'localInfo'
            ]),
            currentPage(){
                if(_.isEmpty(this.listInfo)) return 1
                return this.listInfo.pageNum
            },
            hasUserCount(){
                return !_.isEmpty(this.userCount)
            },
            noEditObject(){
                return _.isEmpty(this.editObject)
            },
            noInvoiceInfo(){
                return _.isEmpty(this.invoiceInfo)
            }
        },
        methods:{
            // actions
            ...mapActions([
                'showLoading',
                'hideLoading',
                'setMsgTip'
            ]),

            // filters
            timestampToDate(timestamp){
                if(!timestamp) return this.placeholder
                return moment(timestamp).format('YYYY-MM-DD')
            },

            // function
            goDetail(id){
                this.$router.replace({
                    name:'finance-in',
                    query:_.merge({},this.$route.query,{
                        num:this.listInfo.pageNum
                    })
                })
                this.$router.push({
                    name:'finance-detail',
                    params:{
                        id:id
                    }
                })
            },
            getList(num = 1){
                if(this.type && !this.phone){
                    this.setMsgTip({
                        text:'请输入手机号',
                        type:'warn'
                    })
                    return
                }
                this.loading = true
                financeApi.historyCheckList({
                    pageSize:10,
                    pageIndex:num,
                    lendType:4,
                    phone:this.phone
                })
                .then(result=>{
                    this.loading = false
                    this.listInfo = result
                    this.records = result.records
                    if(result.totalPage < result.pageNum){
                        this.getList(result.totalPage)
                    }
                })
                .catch(error => {
                    console.error(error)
                    this.loading = false
                })
            },
            init(){
                this.type = this.$route.query.type
                let num = this.$route.query.num
                if(!this.type){
                    this.getList(num)
                }
                else{
                    this.info = {}
                    this.records = []
                }
            }
        },
        created(){
            this.init()
        },
        watch: {
            '$route' (to, from) {
                this.init()
            }
        }
    }
</script>

<style lang="scss">
    @import "../sass/variables";
    @import "../sass/extends";
    @import "../sass/mixins";

    #finance-reject{

    }
</style>
